package util;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import core.Config;

/**
 * Classe responsavel por criar arquivos de log.
 * construtor passa como parametro o nome do arquivo que sera criado e o 
 * workerId que criou esse arquivo.
 * 
 * @author robertow
 *
 */
public class FileLog {

	private String workerId;
	private String nameFile;
	private FileWriter fileWriter;
	private PrintWriter filePrint;
	
	public FileLog(String nameFile, String workerId) {
		this.setWorkerId(workerId);
		this.setNameFile(nameFile);
		try {
			this.fileWriter = new FileWriter(new File(Config.LOG_DIR + this.getNameFile() + "-" + this.getWorkerId() + ".txt"));
			this.filePrint = new PrintWriter(fileWriter);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		this.print("WORKERID \t\t JOBID \t\t JOBSIZE \t\t RESULT \t\t EXECUTIONTIME");
	}
	
	public void print(String str) {	
		try {
			this.fileWriter = new FileWriter(new File(Config.LOG_DIR + this.getNameFile() + "-" + this.getWorkerId() + ".txt"),true);
			this.filePrint = new PrintWriter(fileWriter);
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		this.filePrint.println(str);
		this.filePrint.close();
		try {
			this.fileWriter.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void setWorkerId(String workerId) {
		this.workerId = workerId;
	}
	
	public String getWorkerId() {
		return this.workerId;
	}
	
	public void setNameFile(String nameFile) {
		this.nameFile = nameFile;
	}
	
	public String getNameFile() {
		return this.nameFile;
	}
}
